package com.piggy.logi.sys.controller;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiCarrierDriverExtEditBo;
import com.piggy.logi.sys.bo.LogiDriverCanScheduleQueryBo;
import com.piggy.logi.sys.vo.LogiCarrierDriverExtVo;
import com.piggy.logi.sys.vo.LogiPickupOrderExtVo;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.logi.sys.vo.LogiCarrierDriverVo;
import com.piggy.logi.sys.bo.LogiCarrierDriverQueryBo;
import com.piggy.logi.sys.service.ILogiCarrierDriverService;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.piggy.common.core.domain.R;

import java.lang.Integer;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

/**
 * 承运商司机信息Controller
 */
@Api(value = "承运商司机信息控制器", tags = {"承运商司机信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/logi/carrierDriver")
public class LogiCarrierDriverController extends BaseController {

    private final ILogiCarrierDriverService iLogiCarrierDriverService;

    /**
     * 查询承运商司机信息列表
     */
    @ApiOperation("查询承运商司机信息列表")
    //@SaCheckPermission("@ss.hasPermi('sys:driver:list')")
    @PostMapping("/list")
    public TableDataInfo<LogiCarrierDriverExtVo> list(@Validated @RequestBody LogiCarrierDriverQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return iLogiCarrierDriverService.queryPageList(bo);
    }

    /**
     * 修改承运商司机信息
     */
    @ApiOperation("根据登录用户获取司机信息")
    //@SaCheckPermission("@ss.hasPermi('sys:driver:edit')")
    @GetMapping("/getByLoginId/{userId}")
    public R<LogiCarrierDriverExtVo> getByLoginId(@PathVariable("userId")Long userId) {
        List<LogiCarrierDriverExtVo> voList = iLogiCarrierDriverService.queryList(new LogiCarrierDriverQueryBo().setUserId(userId));
        if (CollUtil.isNotEmpty(voList)) {
            return R.ok(voList.get(0));
        }
        return R.fail("未找到司机信息");
    }

    /**
     * 导出承运商司机信息列表
     */
    @ApiOperation("导出承运商司机信息列表")
    //@SaCheckPermission("@ss.hasPermi('sys:driver:export')")
    @LogiLog(title = "承运商司机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated @RequestBody LogiCarrierDriverQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiCarrierDriverExtVo> list = iLogiCarrierDriverService.queryList(bo);
        ExcelUtil<LogiCarrierDriverExtVo> util = new ExcelUtil<>(LogiCarrierDriverExtVo.class);
        util.exportExcel(response, list, "承运商司机信息");
    }

    /**
     * 获取承运商司机信息详细信息
     */
    @ApiOperation("获取承运商司机信息详细信息")
    //@SaCheckPermission("@ss.hasPermi('sys:driver:query')")
    @GetMapping("/{id}")
    public R<LogiCarrierDriverVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable("id") Long id) {
        return R.ok(iLogiCarrierDriverService.queryById(id));
    }

    /**
     * 新增承运商司机信息
     */
    @ApiOperation("新增承运商司机信息")
    //@SaCheckPermission("@ss.hasPermi('sys:driver:add')")
    @LogiLog(title = "承运商司机信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Integer> add(@Validated @RequestBody LogiCarrierDriverExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setMainDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiCarrierDriverService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改承运商司机信息
     */
    @ApiOperation("修改承运商司机信息")
    //@SaCheckPermission("@ss.hasPermi('sys:driver:edit')")
    @LogiLog(title = "承运商司机信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Integer> edit(@Validated @RequestBody LogiCarrierDriverExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setMainDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiCarrierDriverService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除承运商司机信息
     */
    @ApiOperation("删除承运商司机信息")
    //@SaCheckPermission("@ss.hasPermi('sys:driver:remove')")
    @LogiLog(title = "承运商司机信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                             @PathVariable Long[] ids) {
        return R.ok(iLogiCarrierDriverService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 查询可调度的司机列表
     */
    @ApiOperation("查询可调度的司机列表")
    @PostMapping("/canScheduleList")
    public List<LogiCarrierDriverExtVo> canScheduleList(@Validated @RequestBody LogiDriverCanScheduleQueryBo bo) {
        bo.setDeptId(SecurityUtils.getMainDeptId());
        return iLogiCarrierDriverService.canScheduleList(bo);
    }

}
